{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 08 Scikit-learn中的Scaler"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from sklearn import datasets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "iris = datasets.load_iris()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "X = iris.data\n",
    "y = iris.target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[5.1, 3.5, 1.4, 0.2],\n       [4.9, 3. , 1.4, 0.2],\n       [4.7, 3.2, 1.3, 0.2],\n       [4.6, 3.1, 1.5, 0.2],\n       [5. , 3.6, 1.4, 0.2],\n       [5.4, 3.9, 1.7, 0.4],\n       [4.6, 3.4, 1.4, 0.3],\n       [5. , 3.4, 1.5, 0.2],\n       [4.4, 2.9, 1.4, 0.2],\n       [4.9, 3.1, 1.5, 0.1]])"
     },
     "metadata": {},
     "execution_count": 4
    }
   ],
   "source": [
    "X[:10,:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=666)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### scikit-learn中的StandardScaler"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# standardscaler进行归一化\n",
    "from sklearn.preprocessing import StandardScaler "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "standardScalar = StandardScaler() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "StandardScaler(copy=True, with_mean=True, with_std=True)"
     },
     "metadata": {},
     "execution_count": 9
    }
   ],
   "source": [
    "standardScalar.fit(X_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([5.83416667, 3.08666667, 3.70833333, 1.17      ])"
     },
     "metadata": {},
     "execution_count": 10
    }
   ],
   "source": [
    "# 各个特征相应的均值\n",
    "# mean_不是用户的变量，所以加一个_\n",
    "standardScalar.mean_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([0.81019502, 0.44327067, 1.76401924, 0.75317107])"
     },
     "metadata": {},
     "execution_count": 11
    }
   ],
   "source": [
    "# scale就是标准差，可以用来描述数据分布范围\n",
    "standardScalar.scale_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[-0.90616043,  0.93246262, -1.30856471, -1.28788802],\n       [-1.15301457, -0.19551636, -1.30856471, -1.28788802],\n       [-0.16559799, -0.64670795,  0.22203084,  0.17260355],\n       [ 0.45153738,  0.70686683,  0.95898425,  1.50032315],\n       [-0.90616043, -1.32349533, -0.40154513, -0.09294037],\n       [ 1.43895396,  0.25567524,  0.56216318,  0.30537551],\n       [ 0.3281103 , -1.09789954,  1.0723617 ,  0.30537551],\n       [ 2.1795164 , -0.19551636,  1.63924894,  1.23477923],\n       [-0.78273335,  2.2860374 , -1.25187599, -1.42065998],\n       [ 0.45153738, -2.00028272,  0.44878573,  0.43814747],\n       [ 1.80923518, -0.42111215,  1.46918276,  0.83646335],\n       [ 0.69839152,  0.25567524,  0.90229552,  1.50032315],\n       [ 0.20468323,  0.70686683,  0.44878573,  0.57091943],\n       [-0.78273335, -0.87230374,  0.10865339,  0.30537551],\n       [-0.53587921,  1.38365421, -1.25187599, -1.28788802],\n       [-0.65930628,  1.38365421, -1.25187599, -1.28788802],\n       [-1.0295875 ,  0.93246262, -1.19518726, -0.75680017],\n       [-1.77014994, -0.42111215, -1.30856471, -1.28788802],\n       [-0.04217092, -0.87230374,  0.10865339,  0.03983159],\n       [-0.78273335,  0.70686683, -1.30856471, -1.28788802],\n       [-1.52329579,  0.70686683, -1.30856471, -1.15511606],\n       [ 0.82181859,  0.25567524,  0.78891808,  1.10200727],\n       [-0.16559799, -0.42111215,  0.27871956,  0.17260355],\n       [ 0.94524567, -0.19551636,  0.39209701,  0.30537551],\n       [ 0.20468323, -0.42111215,  0.44878573,  0.43814747],\n       [-1.39986872,  0.25567524, -1.19518726, -1.28788802],\n       [-1.15301457,  1.15805842, -1.30856471, -1.42065998],\n       [ 1.06867274,  0.03007944,  1.0723617 ,  1.63309511],\n       [ 0.57496445, -0.87230374,  0.67554063,  0.83646335],\n       [ 0.3281103 , -0.64670795,  0.56216318,  0.03983159],\n       [ 0.45153738, -0.64670795,  0.6188519 ,  0.83646335],\n       [-0.16559799,  2.96282478, -1.25187599, -1.0223441 ],\n       [ 0.57496445, -1.32349533,  0.67554063,  0.43814747],\n       [ 0.69839152, -0.42111215,  0.33540828,  0.17260355],\n       [-0.90616043,  1.60925001, -1.02512109, -1.0223441 ],\n       [ 1.19209981, -0.64670795,  0.6188519 ,  0.30537551],\n       [-0.90616043,  0.93246262, -1.30856471, -1.15511606],\n       [-1.89357701, -0.19551636, -1.47863088, -1.42065998],\n       [ 0.08125616, -0.19551636,  0.78891808,  0.83646335],\n       [ 0.69839152, -0.64670795,  1.0723617 ,  1.23477923],\n       [-0.28902506, -0.64670795,  0.67554063,  1.10200727],\n       [-0.41245214, -1.54909113, -0.00472406, -0.22571233],\n       [ 1.31552689,  0.03007944,  0.67554063,  0.43814747],\n       [ 0.57496445,  0.70686683,  1.0723617 ,  1.63309511],\n       [ 0.82181859, -0.19551636,  1.18573914,  1.36755119],\n       [-0.16559799,  1.60925001, -1.13849854, -1.15511606],\n       [ 0.94524567, -0.42111215,  0.50547446,  0.17260355],\n       [ 1.06867274,  0.48127103,  1.12905042,  1.76586707],\n       [-1.27644165, -0.19551636, -1.30856471, -1.42065998],\n       [-1.0295875 ,  1.15805842, -1.30856471, -1.28788802],\n       [ 0.20468323, -0.19551636,  0.6188519 ,  0.83646335],\n       [-1.0295875 , -0.19551636, -1.19518726, -1.28788802],\n       [ 0.3281103 , -0.19551636,  0.67554063,  0.83646335],\n       [ 0.69839152,  0.03007944,  1.01567297,  0.83646335],\n       [-0.90616043,  1.38365421, -1.25187599, -1.0223441 ],\n       [-0.16559799, -0.19551636,  0.27871956,  0.03983159],\n       [-1.0295875 ,  0.93246262, -1.36525344, -1.15511606],\n       [-0.90616043,  1.60925001, -1.25187599, -1.15511606],\n       [-1.52329579,  0.25567524, -1.30856471, -1.28788802],\n       [-0.53587921, -0.19551636,  0.44878573,  0.43814747],\n       [ 0.82181859, -0.64670795,  0.50547446,  0.43814747],\n       [ 0.3281103 , -0.64670795,  0.16534211,  0.17260355],\n       [-1.27644165,  0.70686683, -1.19518726, -1.28788802],\n       [-0.90616043,  0.48127103, -1.13849854, -0.88957213],\n       [-0.04217092, -0.87230374,  0.78891808,  0.96923531],\n       [-0.28902506, -0.19551636,  0.22203084,  0.17260355],\n       [ 0.57496445, -0.64670795,  0.78891808,  0.43814747],\n       [ 1.06867274,  0.48127103,  1.12905042,  1.23477923],\n       [ 1.68580811, -0.19551636,  1.18573914,  0.57091943],\n       [ 1.06867274, -0.19551636,  0.8456068 ,  1.50032315],\n       [-1.15301457,  0.03007944, -1.25187599, -1.42065998],\n       [-1.15301457, -1.32349533,  0.44878573,  0.70369139],\n       [-0.16559799, -1.32349533,  0.73222935,  1.10200727],\n       [-1.15301457, -1.54909113, -0.23147896, -0.22571233],\n       [-0.41245214, -1.54909113,  0.05196466, -0.09294037],\n       [ 1.06867274, -1.32349533,  1.18573914,  0.83646335],\n       [ 0.82181859, -0.19551636,  1.01567297,  0.83646335],\n       [-0.16559799, -1.09789954, -0.11810151, -0.22571233],\n       [ 0.20468323, -2.00028272,  0.73222935,  0.43814747],\n       [ 1.06867274,  0.03007944,  0.56216318,  0.43814747],\n       [-1.15301457,  0.03007944, -1.25187599, -1.28788802],\n       [ 0.57496445, -1.32349533,  0.73222935,  0.96923531],\n       [-1.39986872,  0.25567524, -1.36525344, -1.28788802],\n       [ 0.20468323, -0.87230374,  0.78891808,  0.57091943],\n       [-0.04217092, -1.09789954,  0.16534211,  0.03983159],\n       [ 1.31552689,  0.25567524,  1.12905042,  1.50032315],\n       [-1.77014994, -0.19551636, -1.36525344, -1.28788802],\n       [ 1.56238103, -0.19551636,  1.24242787,  1.23477923],\n       [ 1.19209981,  0.25567524,  1.24242787,  1.50032315],\n       [-0.78273335,  0.93246262, -1.25187599, -1.28788802],\n       [ 2.54979762,  1.60925001,  1.52587149,  1.10200727],\n       [ 0.69839152, -0.64670795,  1.0723617 ,  1.36755119],\n       [-0.28902506, -0.42111215, -0.06141278,  0.17260355],\n       [-0.41245214,  2.51163319, -1.30856471, -1.28788802],\n       [-1.27644165, -0.19551636, -1.30856471, -1.15511606],\n       [ 0.57496445, -0.42111215,  1.0723617 ,  0.83646335],\n       [-1.77014994,  0.25567524, -1.36525344, -1.28788802],\n       [-0.53587921,  1.8348458 , -1.13849854, -1.0223441 ],\n       [-1.0295875 ,  0.70686683, -1.19518726, -1.0223441 ],\n       [ 1.06867274, -0.19551636,  0.73222935,  0.70369139],\n       [-0.53587921,  1.8348458 , -1.36525344, -1.0223441 ],\n       [ 2.30294347, -0.64670795,  1.69593766,  1.10200727],\n       [-0.28902506, -0.87230374,  0.27871956,  0.17260355],\n       [ 1.19209981, -0.19551636,  1.01567297,  1.23477923],\n       [-0.41245214,  0.93246262, -1.36525344, -1.28788802],\n       [-1.27644165,  0.70686683, -1.02512109, -1.28788802],\n       [-0.53587921,  0.70686683, -1.13849854, -1.28788802],\n       [ 2.30294347,  1.60925001,  1.69593766,  1.36755119],\n       [ 1.31552689,  0.03007944,  0.95898425,  1.23477923],\n       [-0.28902506, -1.32349533,  0.10865339, -0.09294037],\n       [-0.90616043,  0.70686683, -1.25187599, -1.28788802],\n       [-0.90616043,  1.60925001, -1.19518726, -1.28788802],\n       [ 0.3281103 , -0.42111215,  0.56216318,  0.30537551],\n       [-0.04217092,  2.0604416 , -1.42194216, -1.28788802],\n       [-1.0295875 , -2.45147431, -0.11810151, -0.22571233],\n       [ 0.69839152,  0.25567524,  0.44878573,  0.43814747],\n       [ 0.3281103 , -0.19551636,  0.50547446,  0.30537551],\n       [ 0.08125616,  0.25567524,  0.6188519 ,  0.83646335],\n       [ 0.20468323, -2.00028272,  0.16534211, -0.22571233],\n       [ 1.93266225, -0.64670795,  1.35580532,  0.96923531]])"
     },
     "metadata": {},
     "execution_count": 12
    }
   ],
   "source": [
    "# 进行归一化\n",
    "standardScalar.transform(X_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[5.1, 3.5, 1.4, 0.2],\n       [4.9, 3. , 1.4, 0.2],\n       [5.7, 2.8, 4.1, 1.3],\n       [6.2, 3.4, 5.4, 2.3],\n       [5.1, 2.5, 3. , 1.1],\n       [7. , 3.2, 4.7, 1.4],\n       [6.1, 2.6, 5.6, 1.4],\n       [7.6, 3. , 6.6, 2.1],\n       [5.2, 4.1, 1.5, 0.1],\n       [6.2, 2.2, 4.5, 1.5]])"
     },
     "metadata": {},
     "execution_count": 13
    }
   ],
   "source": [
    "# 原数据不会改变\n",
    "X_train[:10,:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 所以要保存归一化之后的数据\n",
    "X_train = standardScalar.transform(X_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[-0.90616043,  0.93246262, -1.30856471, -1.28788802],\n       [-1.15301457, -0.19551636, -1.30856471, -1.28788802],\n       [-0.16559799, -0.64670795,  0.22203084,  0.17260355],\n       [ 0.45153738,  0.70686683,  0.95898425,  1.50032315],\n       [-0.90616043, -1.32349533, -0.40154513, -0.09294037],\n       [ 1.43895396,  0.25567524,  0.56216318,  0.30537551],\n       [ 0.3281103 , -1.09789954,  1.0723617 ,  0.30537551],\n       [ 2.1795164 , -0.19551636,  1.63924894,  1.23477923],\n       [-0.78273335,  2.2860374 , -1.25187599, -1.42065998],\n       [ 0.45153738, -2.00028272,  0.44878573,  0.43814747]])"
     },
     "metadata": {},
     "execution_count": 15
    }
   ],
   "source": [
    "X_train[:10,:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 对测试数据进行归一化\n",
    "X_test_standard = standardScalar.transform(X_test) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[-0.28902506, -0.19551636,  0.44878573,  0.43814747],\n       [-0.04217092, -0.64670795,  0.78891808,  1.63309511],\n       [-1.0295875 , -1.77468693, -0.23147896, -0.22571233],\n       [-0.04217092, -0.87230374,  0.78891808,  0.96923531],\n       [-1.52329579,  0.03007944, -1.25187599, -1.28788802],\n       [-0.41245214, -1.32349533,  0.16534211,  0.17260355],\n       [-0.16559799, -0.64670795,  0.44878573,  0.17260355],\n       [ 0.82181859, -0.19551636,  0.8456068 ,  1.10200727],\n       [ 0.57496445, -1.77468693,  0.39209701,  0.17260355],\n       [-0.41245214, -1.09789954,  0.39209701,  0.03983159]])"
     },
     "metadata": {},
     "execution_count": 17
    }
   ],
   "source": [
    "X_test_standard[:10,:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用归一化后的数据进行knn分类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "knn_clf = KNeighborsClassifier(n_neighbors=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',\n                     metric_params=None, n_jobs=None, n_neighbors=3, p=2,\n                     weights='uniform')"
     },
     "metadata": {},
     "execution_count": 20
    }
   ],
   "source": [
    "knn_clf.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "1.0"
     },
     "metadata": {},
     "execution_count": 21
    }
   ],
   "source": [
    "knn_clf.score(X_test_standard, y_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "注意，此时不能传入没有归一化的数据！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "0.3333333333333333"
     },
     "metadata": {},
     "execution_count": 22
    }
   ],
   "source": [
    "# 用归一化数据集训练出来的模型绝对不能传入没有归一化的数据\n",
    "knn_clf.score(X_test, y_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 实现我们自己的standardScaler"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "代码参见：[这里](playML/preprocessing.py)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=666)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "<playML.preprocessing.StandardScaler at 0x1ce648d5808>"
     },
     "metadata": {},
     "execution_count": 24
    }
   ],
   "source": [
    "from playML.preprocessing import StandardScaler\n",
    "\n",
    "my_standardScalar = StandardScaler() \n",
    "my_standardScalar.fit(X_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([5.83416667, 3.08666667, 3.70833333, 1.17      ])"
     },
     "metadata": {},
     "execution_count": 25
    }
   ],
   "source": [
    "my_standardScalar.mean_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([0.81019502, 0.44327067, 1.76401924, 0.75317107])"
     },
     "metadata": {},
     "execution_count": 26
    }
   ],
   "source": [
    "my_standardScalar.scale_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "X_train = standardScalar.transform(X_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[-0.90616043,  0.93246262, -1.30856471, -1.28788802],\n       [-1.15301457, -0.19551636, -1.30856471, -1.28788802],\n       [-0.16559799, -0.64670795,  0.22203084,  0.17260355],\n       [ 0.45153738,  0.70686683,  0.95898425,  1.50032315],\n       [-0.90616043, -1.32349533, -0.40154513, -0.09294037],\n       [ 1.43895396,  0.25567524,  0.56216318,  0.30537551],\n       [ 0.3281103 , -1.09789954,  1.0723617 ,  0.30537551],\n       [ 2.1795164 , -0.19551636,  1.63924894,  1.23477923],\n       [-0.78273335,  2.2860374 , -1.25187599, -1.42065998],\n       [ 0.45153738, -2.00028272,  0.44878573,  0.43814747]])"
     },
     "metadata": {},
     "execution_count": 28
    }
   ],
   "source": [
    "X_train[:10,:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Scikit-Learn中的最值归一化"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "MinMaxScaler: [http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.MinMaxScaler.html](http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.MinMaxScaler.html)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "练习：同学们也可以尝试实现自己的MinMaxScaler:)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.6.8 64-bit ('anaconda': conda)",
   "language": "python",
   "name": "python36864bitanacondaconda085db2e8b14649f4b32196068f7124e9"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7-final"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}